package com.hotel.controllers;

import com.hotel.entitys.Dept;
import com.hotel.entitys.Position;
import com.hotel.entitys.RestPOJO;
import com.hotel.services.DeptService;
import com.hotel.services.PositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="职位管理接口")//tags:说明信息

public class PositionController {
    @Autowired
    private PositionService positionService;
    @Autowired
    private DeptService deptService;

    @ApiOperation(
            value = "查询所有职位部门",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/position")
    public RestPOJO getAllPosition(){
        RestPOJO restPOJO = new RestPOJO();
        List<Position> list = positionService.getAllPosition();
        if (list.size()>0){
            restPOJO.setMessage("查询职位部门成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);
        } else {
            restPOJO.setMessage("查询职位部门失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询职位对应部门",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/position/dept")
    public RestPOJO getPositionDept(){
        RestPOJO restPOJO = new RestPOJO();
        List<Position> positionlist = positionService.getAllPosition();
        List<Dept> deptList = deptService.getAllDepts();
        Map<String,List> map = new HashMap<String, List>();
        map.put("positionList",positionlist);
        map.put("deptList",deptList);
        if (map.size()>0){
            restPOJO.setMessage("查询职位部门成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(map);
        } else {
            restPOJO.setMessage("查询职位部门失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "增加",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/position/add")
    public RestPOJO addPosition(@RequestBody Position position){
        RestPOJO restPOJO = new RestPOJO();
        int count = positionService.addPosition(position);
        if (count > 0){
            restPOJO.setMessage("增加职位成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("增加职位失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    // 获取单个员工信息
    @ApiOperation(
            value = "获取单个员工信息通过员工主键id",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/position/{id}")
    public RestPOJO getSinglePosition(@PathVariable("id") Integer position_id){
        RestPOJO restPOJO = new RestPOJO();
        Position position = positionService.getPositionById(position_id);
        restPOJO.setData(position);
        return restPOJO;
    }
    // 修改员工信息
    @ApiOperation(
            value = "修改员工信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PutMapping("/position")
    public RestPOJO updatePosition(@RequestBody Position position){
        RestPOJO restPOJO = new RestPOJO();
        int count = positionService.changePosition(position);
        if (count > 0){
            restPOJO.setMessage("修改职位成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("修改职位失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "删除员工信息",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @DeleteMapping("/position/{id}")
    public RestPOJO deleteSinglePosition(@PathVariable("id") Integer position_id){
        RestPOJO restPOJO = new RestPOJO();
        int count = positionService.deleteOne(position_id);
        if (count > 0){
            restPOJO.setMessage("删除职位成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("删除职位失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "批量删除员工信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/position/delAll")
    public RestPOJO deleteMultiPosition(@RequestBody Object[] ids){
        RestPOJO restPOJO = new RestPOJO();
        int count = positionService.deleteMulti(ids);
        if (count > 0){
            restPOJO.setMessage("批量删除职位成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("批量删除职位失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
